﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoomPriceEntity ;
using Entity.ProcessResultEntity;
using DataLoader;
using System.Xml;

namespace PriceCacheDownloader
{
    public class Converter
    {
        IList<ProcessResultHotelEntity> ProcessResultHotelEntityList = null;
        public Converter()
        {
            ProcessResultHotelEntityList= PriceLoader.Get_PreparedHotelList(33
                        , RatePlanCode: "AgodaPP" );
                        
                        
        }

        public string GetHotelCode(int hotel)
        {
            ProcessResultHotelEntity entity = ProcessResultHotelEntityList.Where(p => p.Hotel == hotel).FirstOrDefault();
            if (entity != null) return entity.HotelGroupHotelCode;
            else return string.Empty;
        }

        public List<PriceEntity> GetPriceEntity(string responseXml)
        {
            PriceEntity tempEntity = null;
            List<PriceEntity> result = new List<PriceEntity>();
            XmlDocument xmlDoc = new XmlDocument();
            try
            {
                xmlDoc.LoadXml(responseXml);
            }
            catch (Exception ex)
            {
                return null;
            }
            XmlElement element = xmlDoc.DocumentElement;
            switch (element.Attributes["status"].Value)
            {   
                case "200":
                    break;
                default:
                    return null;
            }
            XmlNodeList nodeList = xmlDoc.DocumentElement.FirstChild.ChildNodes;  //获重酒店列
            foreach (XmlNode hotelNode in nodeList) //酒店级循环
            {
                string xmlHotelCode = hotelNode["Id"].InnerText;

                //(该酒店)携程“接口房型匹配表”的每个RoomType+RatePlanCode数据
                ProcessResultHotelEntity hotelEntity = ProcessResultHotelEntityList.Where(p => p.HotelGroupHotelCode == xmlHotelCode).FirstOrDefault();
                if (hotelEntity == null) continue;

                foreach (XmlNode roomNodeIn in hotelNode["Rooms"].ChildNodes)
                {
                    RoomAllInfoEntity roomObj
                        = hotelEntity.RoomInfoList.Where(p => p.RoomTypeCode == roomNodeIn.Attributes["id"].Value).FirstOrDefault();
                    if (roomObj == null) continue;
                    XmlNodeList DailyRateDateList = roomNodeIn["RateInfo"]["DailyRates"].ChildNodes;
                    foreach (XmlNode DailyRateDateNode in DailyRateDateList)
                    {
                        double exclusive = DailyRateDateNode.Attributes["exclusive"] != null ? double.Parse(DailyRateDateNode.Attributes["exclusive"].Value) : 0;
                        double inclusive = DailyRateDateNode.Attributes["inclusive"] != null ? double.Parse(DailyRateDateNode.Attributes["inclusive"].Value) : 0;
                        tempEntity = new PriceEntity()
                        {
                            Room = roomObj.Room,
                            Price = inclusive,
                            //Cost = 
                        };
                        result.Add(tempEntity);
                        break;
                    }
                }
            }
            return result;
        }
    }
}
